package cn.mlong.demo.install

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.*
import org.jdom.Element
import com.intellij.util.xmlb.annotations.Attribute
@State(
    name = "AdbSettings",
    storages = [Storage("AdbSettings.xml")]
)
@Service(Service.Level.APP)
class AdbSettings : PersistentStateComponent<Element> {
    @Attribute
    var adbPath: String = "adb"

    @Attribute
    var installCommand: String = "install -r -t"

    override fun getState(): Element {
        return Element("settings").apply {
            setAttribute("adbPath", adbPath)
            setAttribute("installCommand", installCommand)
        }
    }

    override fun loadState(state: Element) {
        state.getAttributeValue("adbPath")?.let { adbPath = it }
        state.getAttributeValue("installCommand")?.let { installCommand = it }
    }

    fun getFullInstallCommand(apkPath: String): String {
        return "$adbPath $installCommand \"$apkPath\""
    }

    companion object {
        val instance: AdbSettings
            get() = ApplicationManager.getApplication().getService(AdbSettings::class.java)
    }
}